
#include "ecs/time.hpp"

namespace qbe::ecs {

ThreadPool* ThreadPool::getInstance() {
  if (instance == nullptr) {
    instance = new ThreadPool();
  }
  return instance;
};

// 线程池
ThreadPool* ThreadPool::instance = nullptr;

std::chrono::duration<double, std::ratio<1>> Time::startup() {
  return std::chrono::high_resolution_clock::now() - _first_update;
};

void Time::update_with_instant(std::chrono::time_point<std::chrono::high_resolution_clock>&& instant) {
  _last_update_delta = instant - _last_update;
  _last_update = instant;
};

std::chrono::duration<double, std::ratio<1>> Time::delta() {
  return _last_update_delta;
};

double Time::delta_seconds() {
  return _last_update_delta.count();
};

void Time::Start() {
  start_time = std::chrono::high_resolution_clock::now();
  is_running = true;
}

double Time::TotalTime() const {
  if (!is_running) {
    return 0;
    // throw std::runtime_error("Timer is not running. Call Start() first.");
  }
  auto now = std::chrono::high_resolution_clock::now();
  return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
}

double Time::End() {
  double res = TotalTime();
  is_running = false;
  return res;
}

}  // namespace qbe::ecs